import java.sql.Time;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 
 */

/**
 * @author florian
 *
 */
public class CommandLine 
{
	
	/**
	 * @param login
	 * @param password
	 * @param runners
	 * @return the runner if found
	 */
	static public Runner testConnection(String login, String password, ArrayList<Runner> runners)
	{
		Runner res = null;
		for(int i=0; i<runners.size() && res==null; i++)
		{
			if(runners.get(i)._login.equals(login) && runners.get(i)._password.equals(password))
			{
				res = runners.get(i);
			}
		}
		return res;
	}
	
	
	/**
	 * @param login
	 * @param runners
	 * @return true if the login already exists.
	 */
	public static boolean testNewLogin(String login, ArrayList<Runner> runners)
	{
		boolean res = false;
		for(int i=0; i<runners.size() && !res; i++)
		{
			res = runners.get(i)._login.equals(login);
		}
		return res;
	}
	
	public static boolean testGroupName(String groupName, ArrayList<Group> groups) 
	{
		boolean res = false;
		for(int i=0; i<groups.size() && !res; i++)
		{
			res = groups.get(i)._group_name.equals(groupName);
		}
		return res;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		ArrayList<Runner> runners = new ArrayList<Runner>();
		ArrayList<Group> groups = new ArrayList<Group>();

		
		Runner admin = new Runner("admin", "admin");
		runners.add(admin);
		Group admin_group = new Group("admin",admin);
		groups.add(admin_group);
		
		
		Runner current_runner = admin;
		current_runner._group = admin_group;
		Group current_group = null;
		
		System.out.println("Social sport : 'Preparing for Helsinki city run'");
		
		// Create a scanner to read from keyboard
	    Scanner scanner = new Scanner (System.in);
	    while (true)
	    {
		    int int_val=0;
		    while (int_val==0)
		    {
		    	System.out.println("Please choose:\n"+
		    					   "1.............log in\n"+
		    					   "2.............sign in\n"+
		    					   "3.............exit");
			    try 
			    {
			    	
			        int_val = Integer.parseInt(scanner.next());
			        if(int_val>3)
			        {
			        	int_val=0;
			        	throw new NumberFormatException();
			        }
			    }
			    catch  (NumberFormatException e) 
			    {
			        System.out.println ("Error: 1, 2 or 3 expected");  
			    }
		    }
		    boolean connected = false;
		    while(!connected)
		    {
			    if (int_val==1)
			    {
			    	System.out.println("login: ");
			    	String enterred_login = scanner.next();
			    	System.out.println("password: ");
			    	String enterred_password = scanner.next();
			    	
			    	current_runner = testConnection(enterred_login, enterred_password, runners);
			    	if(current_runner!=null)
			    	{
			    		connected = true;
			    		if(current_runner._group!=null)
			    		{
			    			current_group=current_runner._group;
			    		}
			    	}
			    	if(!connected)
			    	{
			    		System.out.println("Warning: wrong login or password");
			    	}
			    }
			    else if (int_val==2)
			    {
			    	System.out.println("Choose a new login: ");
			    	String new_login = scanner.next();
			    	if(!testNewLogin(new_login, runners))
			    	{
			    		System.out.println("Do you really want to use this login: "+new_login);
			    		
			    		int confirm_login = 0;
			    		while(confirm_login == 0)
			    		{
				    		System.out.println("Press:\n"+
				    						   "1.............confirm\n"+
				    						   "2.............choose another one.");
				    		try
				    		{
				    			confirm_login = Integer.parseInt(scanner.next());
				    		}
				    		catch (NumberFormatException e)
				    		{
				    			System.out.println ("Please choose between 1 to confirm or 2 to change");
				    		}
			    		}
			    		if (confirm_login==1)
			    		{
			    			boolean paswd_ok = false;
			    			while (!paswd_ok)
			    			{
			    				System.out.println("Choose a new password: ");
				    			String new_password = scanner.next();
				    			System.out.println("Enter again your password: ");
				    			if(new_password.equals(scanner.next()))
				    			{
				    				System.out.println("New runner account created. Your login is "+new_login);
				    				paswd_ok=true;
				    				Runner r = new Runner(new_login, new_password);
				    				runners.add(r);
				    				connected = true;
				    				current_runner = r;
				    			}
				    			else
				    			{
				    				System.out.println("2nd password doesn't match. Please try again.");
				    			}
			    			}	
			    		}
			    	}
			    	else
			    	{
			    		System.out.println("Login already used, please choose an other login.");
			    	}
			    	
			    }else if(int_val==3)
			    {
			    	System.exit(0);
			    }
		    }
		    System.out.println("Welcome "+current_runner._login+" in Social sport.");
		    
		    
		    int menu_choice=0;
		    while (menu_choice==0){
			    while (menu_choice==0)
			    {
			    	System.out.println("Menu (choose the menu number):\n"+
							"11............Create group\n"+
							"12............Join group\n"+
							"13............View group\n"+
							"14............Leave group\n"+
							"2.............Add training\n"+
							"3.............View profile\n"+
							"41............Compare individual stats\n"+
							"42............Compare group stats\n"+
							"5.............View Ranking\n"+
							"6.............Log out\n"+
							"7.............Exit");
				    try 
				    {
				        menu_choice = Integer.parseInt(scanner.next());
				    }
				    catch  (NumberFormatException e) 
				    {
				        System.out.println ("Error: wrong menu number.");  
				    }
			    }
			    
			    switch(menu_choice)
			    {
			    case 11:
			    	System.out.println("Create group menu:");
			    	System.out.println("Choose a new group name: ");
			    	String group_name = scanner.next();
			    	if(!testGroupName(group_name, groups))
			    	{
			    		System.out.println("Do you really want to use this name for the group: "+group_name);
			    		
			    		int confirm_group_name = 0;
			    		while(confirm_group_name == 0)
			    		{
			    			System.out.println("Press:\n"+
		    						   "1.............confirm\n"+
		    						   "2.............choose another one.");
				    		try
				    		{
				    			confirm_group_name = Integer.parseInt(scanner.next());
				    		}
				    		catch (NumberFormatException e)
				    		{
				    			System.out.println ("Please choose between 1 to confirm or 2 to change");
				    		}
			    		}
			    		if (confirm_group_name==1)
			    		{
		    				System.out.println("New group created: "+group_name);
		    				Group g = new Group(group_name, current_runner);
		    				groups.add(g);
		    				current_group = g;
			    		}
			    	}
			    	else
			    	{
			    		System.out.println("Login already used, please choose an other login.");
			    	}
			    	menu_choice=0;
			    	break;
			    	
			    case 12:
			    	System.out.println("Join group menu:");
			    	if(current_runner._group!=null)
			    	{
			    		System.out.println("Sorry, you already belong to a group.\n"+
			    							"If you want to join this group, you first have to leave your current group: "+current_runner._group._group_name);
			    	}
			    	else
			    	{
			    		System.out.println("List of existing groups");
			    		for(int i=0; i<groups.size(); i++)
			    		{
			    			System.out.println(i+" - "+groups.get(i)._group_name);
			    		}
			    		
			    		int group_number = -1;
			    		while(group_number == -1)
			    		{
			    			System.out.println("Please choose the number of the group you want to join:");
				    		try
				    		{
				    			int tmp =Integer.parseInt(scanner.next());
				    			if (tmp<groups.size() && tmp>=0)
				    			{
				    				group_number = tmp;
				    			}
				    			else
				    			{
				    				throw new NumberFormatException();
				    			}
				    		}
				    		catch (NumberFormatException e)
				    		{
				    			System.out.println ("Please choose between 0 and "+ (groups.size()-1));
				    		}
			    		}
			    		int confirm_group_number = 0;
			    		while(confirm_group_number == 0)
			    		{
			    			System.out.println("Press:\n"+
		    						   "1.............confirm\n"+
		    						   "2.............choose another one.");
				    		try
				    		{
				    			confirm_group_number = Integer.parseInt(scanner.next());
				    			(groups.get(group_number))._runners.add(current_runner);
				    			current_group = groups.get(group_number);
				    			current_runner._group = current_group;
				    			System.out.println("Congratulations, you have successfully joined the group: "+current_group._group_name);
				    		}
				    		catch (NumberFormatException e)
				    		{
				    			System.out.println("Press:\n"+
			    						   "1.............confirm\n"+
			    						   "2.............choose another one.");
				    		}
			    		}
			    	}
			    	menu_choice=0;
			    	break;
			    	
			    	
			    case 13:
			    	boolean back = false;
			    	while(!back)
			    	{
				    	System.out.println("List of groups");
			    		for(int i=0; i<groups.size(); i++)
			    		{
			    			System.out.println(i+" - "+groups.get(i)._group_name);
			    		}
			    		
			    		int group_number = -1;
			    		while(group_number == -1)
			    		{
			    			System.out.println("Please choose the number of the group you want to view:");
				    		try
				    		{
				    			int tmp =Integer.parseInt(scanner.next());
				    			if (tmp<groups.size() && tmp>=0)
				    			{
				    				group_number = tmp;
				    			}
				    			else
				    			{
				    				throw new NumberFormatException();
				    			}
				    		}
				    		catch (NumberFormatException e)
				    		{
				    			System.out.println ("Please choose between 0 and "+ (groups.size()-1));
				    		}
			    		}
			    		System.out.println("You are now viewing the group: "+groups.get(group_number)._group_name);
			    		System.out.println("Total score of the group. The runners of this group have run: "+groups.get(group_number)._score+"kms.");
			    		System.out.println("List of all members of the group.");
			    		for(int i=0; i<groups.get(group_number)._runners.size(); i++)
			    		{
			    			System.out.println(groups.get(group_number)._runners.get(i)._login);
			    		}
				    	
				    	int confirm_back = 0;
			    		while(confirm_back == 0)
			    		{
			    			System.out.println("Please choose:\n" +
			    							   "1.............back to groups\n"+
			    							   "2.............back to main menu");
				    		try
				    		{
				    			confirm_back = Integer.parseInt(scanner.next());
				    		}
				    		catch (NumberFormatException e)
				    		{
				    			System.out.println ("Please choose between 1 to go back to groups or 2 to go back to the main menu");
				    		}
			    		}
			    		if(confirm_back==2)
			    		{
			    			back = true;
			    		}
			    	}
			    	menu_choice=0;
			    	break;
			    	
			    case 14:
			    	System.out.println("Leave group menu:");
			    	if(current_runner._group!=null)
			    	{
			    		System.out.println("Sorry, you don't belong to any group.");
			    	}
			    	else
			    	{
			    		System.out.println("You will leave the group: "+current_runner._group._group_name);	    		
			    		int confirm_leave = 0;
			    		while(confirm_leave == 0)
			    		{
			    			System.out.println("Press:\n"+
		    						   "1.............confirm\n"+
		    						   "2.............choose another one.");
				    		try
				    		{
				    			confirm_leave = Integer.parseInt(scanner.next());
				    			for(int i=0; i<groups.size(); i++)
				    			{
				    				if(groups.get(i)==current_group)
				    				{
				    					for(int j=0; j<groups.get(i)._runners.size(); j++)
				    					{
				    						if(groups.get(i)._runners.get(j)==current_runner)
				    						{
				    							groups.get(i)._runners.remove(j);
				    						}
				    					}
				    				}
				    			}
				    			current_group = null;
				    			current_runner._group = null;
				    			System.out.println("Congratulations, you have successfully joined the group: "+current_group._group_name);
				    		}
				    		catch (NumberFormatException e)
				    		{
				    			System.out.println ("Please choose between 1 to confirm or 2 to choose");
				    		}
			    		}
			    	}
			    	menu_choice=0;
			    	break;	    	
			    	
			    case 2:
			    	System.out.println("Add training menu");
			    	System.out.println("Please give a name to your training session:");
			    	String in_name = scanner.next();
			    	System.out.println("Please give the date of your training session:");
			    	String in_date = scanner.next();
			    	double in_distance = 0;
		    		while(in_distance == 0)
		    		{
		    			System.out.println("Please give a distance (just the value in meters) of your training session:");	    		try
			    		{
			    			in_distance = Double.parseDouble(scanner.next());
			    		}
			    		catch (NumberFormatException e)
			    		{
			    			System.out.println ("Please enter a number");
			    		}
		    		}	    	
			    	System.out.println("Please give the duration to your training session:");
			    	String in_duration = scanner.next();
			    	System.out.println("Please give a feedback to your training session:");
			    	String in_feedback = scanner.next();
			    	
			    	current_runner._trainings.add(new Training(in_date, in_name,in_distance, in_duration, in_feedback));
			    	
			    	menu_choice=0;
			    	break;
			    	
			    	
			    case 3:
			    	System.out.println("Your Profile :");
			    	System.out.println(current_runner.ToString());
			    	for(int i=0;i<current_runner._trainings.size();i++)
			    	{
			    		System.out.println(current_runner._trainings.get(i).ToString());
			    	}
			    	break;
			    case 41:
			    	for(int i=0; i<runners.size(); i++)
					{
			    		System.out.println(runners.get(i).ToString());
					}
			    	System.out.println("Enter the name of the runner you want to compare with");
			    	Runner challenger = new Runner();
			    	boolean exist = false;
			    	while(!exist)
			    	{
				    	String challengerName = scanner.next();
				    	for(int i=0; i<runners.size(); i++)
						{
					    	if(runners.get(i)._login.equals(challengerName))	  {
					    		challenger = runners.get(i);
					    		exist = true;
					    	}
						}
				    	if(!exist)
				    	{
				    		System.out.println("please enter a correct runner name");
				    	}
				    	else
				    	{
				    		System.out.println("You : " + current_runner._login + " (score : " + current_runner._score + ")");
				    		System.out.println("Versus : " + challenger._login + " (score : " + challenger._score + ")");
				    		int result = current_runner.compareTo(challenger);
				    		switch(result)
				    		{
				    			case 1: System.out.println("You have PWNED " + challenger._login);
				    			break;
				    			case 0: System.out.println("Round draw between you and " + challenger._login);
				    			break;
				    			case -1: System.out.println("You have been PWNED by " + challenger._login);
				    			break;
				    		}
				    	}
			    	}
		   	
			    	break;
			    case 42:
			    	if(current_runner._group != null)
			    	{
				    	for(int i=0; i<runners.size(); i++)
						{
				    		System.out.println(groups.get(i).ToString());
						}
				    	System.out.println("Enter the name of the group you want your group to be compared with");
				    	Group challenger1 = new Group();
				    	boolean exist1 = false;
				    	while(!exist1)
				    	{
					    	String challengerName = scanner.next();
					    	for(int i=0; i<groups.size(); i++)
							{
						    	if(groups.get(i)._group_name.equals(challengerName))	  {
						    		challenger1 = groups.get(i);
						    		exist1 = true;
						    	}
							}
					    	if(!exist1)
					    	{
					    		System.out.println("please enter a correct group name");
					    	}
					    	else
					    	{
					    		System.out.println("Your group : " + current_runner._group._group_name + " (score : " + current_runner._group._score + ")");
					    		System.out.println("Versus : " + challenger1._group_name + " (score : " + challenger1._score + ")");
					    		int result = current_runner.compareTo(challenger1);
					    		switch(result)
					    		{
					    			case 1: System.out.println("Your group has PWNED " + challenger1._group_name);
					    			break;
					    			case 0: System.out.println("Round draw between your group " + challenger1._group_name);
					    			break;
					    			case -1: System.out.println("Your group has been PWNED by " + challenger1._group_name);
					    			break;
					    		}
					    	}
				    	}		    	
			    	}
			    	break;
			    case 5:
			    	for(int i=0; i<runners.size(); i++)
					{
			    		runners.get(i).updateScore();
					}
			    	Collections.sort(runners, Collections.reverseOrder());
			    	for(int i=0; i<runners.size(); i++)
					{
			    		System.out.println(runners.get(i).ToString());
					}
			    	menu_choice = 0;
			    	break;
			    case 6:
			    	System.out.println("Are you sure you want to log out?");
		    		int confirm_quit = 0;
		    		while(confirm_quit !=1 && confirm_quit != 2)
		    		{
			    		System.out.println("Press:\n"+
			    						   "1.............confirm\n"+
			    						   "2.............go back to menu selection");
			    		try
			    		{
			    			confirm_quit = Integer.parseInt(scanner.next());
			    		}
			    		catch (NumberFormatException e)
			    		{
			    			System.out.println ("Please choose between 1 to confirm or 2 to change");
			    		}
			    		if (confirm_quit !=1 && confirm_quit != 2)
			    			System.out.println ("Please choose between 1 to confirm or 2 to change");
			    		else{
			    			if(confirm_quit == 1)
			    			{
			    				connected = false;
			    				System.out.println("logout confirmed.");
			    			}
			    			if(confirm_quit == 2)
			    			{
			    				menu_choice = 0;
			    				System.out.println("logout cancelled.");
			    			}
			    		}
		    		}
			    	break;
			    case 7:
			    	System.exit(0);
			    default:
			    		break;
			    }
		    }
		}
	}
}









